package net.wantongtech.xa.uploadbasicinfo.dao;

import com.google.common.collect.Sets;
import com.wtkj.dao.impl.BaseDataTemplate;
import com.wtkj.vo.dzzf.share.staticvalue.AgencyType;
import net.wantongtech.xa.uploadbasicinfo.vo.Organization;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Set;

/**
 * 上传基础信息DAO实现
 *
 * @author MaShibo
 * 日期 :  2019/12/17
 */
@Repository
public class UploadBasicInfoDao extends BaseDataTemplate {


    public UploadBasicInfoDao(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }


    /**
     * 查询所有组织结构信息
     *
     * @return 组织机构信息
     */
    public List<Organization> queryAllOrganization() {
        final String sql = "select * from jg_organization";
        return jdbcTemplate.query(sql, BeanPropertyRowMapper.newInstance(Organization.class));
    }


    /**
     * 查询所有的channelId信息
     *
     * @return channelId集合
     */
    public Set<String> queryAllChannelId() {
        final String sql = "SELECT a.channelid FROM jg_organization2 a WHERE a.channelid IS NOT NULL";
        return jdbcTemplate.query(sql, rs -> {
            Set<String> channelIdSet = Sets.newHashSet();
            while (rs.next()) {
                channelIdSet.add(rs.getString(1));
            }
            return channelIdSet;
        });
    }


    /**
     * 根据channelId查询服务网点信息
     *
     * @param channelId 渠道ID
     * @return 网点信息
     */
    public List<Organization> queryOrganizationByChannelId(String channelId) {
        final String sql =
                "SELECT *\n" +
                        "  FROM jg_organization a\n" +
                        " WHERE a.channelid LIKE ?\n" +
                        "   AND a.agencytype <> ?\n" +
                        "   AND (a.description <> 'update' OR a.description IS NULL)";
        return jdbcTemplate.query(sql, BeanPropertyRowMapper.newInstance(Organization.class),
                String.format("%s%%", channelId), AgencyType.AGENCY.getValue());
    }
}
